document.addEventListener("DOMContentLoaded", function () {
        const timeElement = document.querySelector(".time");
        const dateWeekElement = document.querySelector(".date > span:first-child");
        const dateFullElement = document.querySelector(".date > span:last-child");

        const weekdays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];

        // 存储上一次的日期字符串，避免重复更新
        let lastDate = "";

        function updateTime() {
            const now = new Date();

            // 获取时间部分
            const hours = String(now.getHours()).padStart(2, '0');
            const minutes = String(now.getMinutes()).padStart(2, '0');
            const seconds = String(now.getSeconds()).padStart(2, '0');

            // 设置实时时间
            timeElement.textContent = `${hours}:${minutes}:${seconds}`;

            // 获取当前日期和星期
            const year = now.getFullYear();
            const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始
            const day = String(now.getDate()).padStart(2, '0');
            const weekday = weekdays[now.getDay()];
            const currentDate = `${year}年${month}月${day}日`;

            // 如果日期发生变化，更新日期和星期
            if (currentDate !== lastDate) {
                dateWeekElement.textContent = weekday;
                dateFullElement.textContent = currentDate;
                lastDate = currentDate; // 更新记录
            }
        }

        // 初始化时间
        updateTime();

        // 每秒更新一次
        setInterval(updateTime, 1000);
    });

    document.addEventListener("DOMContentLoaded", function () {
        // 配置参数
        const startVal = 0;          // 起始值
        const endVal1 = 3001;         // 结束值
        const endVal2 = 2980;         // 结束值
        const endVal3 = 21;         // 结束值
        const endVal4 = 13;         // 结束值
        const duration1 = 5000;       // 动画持续时间（毫秒）
        const duration2 = 4000;       // 动画持续时间（毫秒）
        const duration3 = 3000;       // 动画持续时间（毫秒）
        const duration4 = 2000;       // 动画持续时间（毫秒）
        const element1 = document.getElementById("count-value1"); // 目标元素
        const element2 = document.getElementById("count-value2"); // 目标元素
        const element3 = document.getElementById("count-value3"); // 目标元素
        const element4 = document.getElementById("count-value4"); // 目标元素

        // 启动动画
        animateCount(startVal, endVal1, duration1, element1);
        animateCount(startVal, endVal2, duration2, element2);
        animateCount(startVal, endVal3, duration3, element3);
        animateCount(startVal, endVal4, duration4, element4);
    });

    // 数字滚动动画函数
    function animateCount(startVal, endVal, duration, element) {
        const startTime = performance.now(); // 获取动画开始时间

        function step(currentTime) {
            const elapsed = currentTime - startTime; // 已经过的时间
            const progress = Math.min(elapsed / duration, 1); // 进度（0~1）

            // 计算当前值
            const currentValue = Math.floor(startVal + (endVal - startVal) * progress);

            // 更新 DOM
            element.textContent = currentValue;

            // 如果动画未完成，继续请求下一帧
            if (progress < 1) {
                requestAnimationFrame(step);
            }
        }

        // 启动第一帧
        requestAnimationFrame(step);
    }

    // 初始化贵州财经大学校园地图
    document.addEventListener("DOMContentLoaded", function () {
        // 创建3D地图
        const map = new AMap.Map('gufe-campus-map', {
            viewMode: '3D', // 使用3D视图
            center: [106.64195, 26.373818], // 贵州财经大学中心坐标
            zoom: 17.2, // 缩放级别
            pitch: 60, // 俯仰角度
            rotation: -15, // 旋转角度
            mapStyle: 'amap://styles/normal', // 初始主题颜色
            // features:['road', 'building', 'point']//
        });

        // 添加校园建筑标记数据
        const buildings = [
            { 
                name: "图书馆", 
                position: [106.643916, 26.378733],
                info: {
                    name: "图书馆",
                    type: "办公楼",
                    faultCount: 15,
                    lastFault: "2025-01-02",
                    faultType: "电力故障",
                    status: "已完成"
                }
            },
            { 
                name: "樱花苑3栋", 
                position: [106.642576, 26.372896],
                info: {
                    name: "樱花苑3栋",
                    type: "宿舍楼",
                    faultCount: 8,
                    lastFault: "2025-01-01",
                    faultType: "空调故障",
                    status: "处理中"
                }
            },
            { 
                name: "勤学楼403", 
                position: [106.641298, 26.377168],
                info: {
                    name: "勤学楼403",
                    type: "教学楼",
                    faultCount: 12,
                    lastFault: "2024-12-30",
                    faultType: "水控故障",
                    status: "已完成"
                }
            },
            { 
                name: "正德楼", 
                position: [106.640622, 26.380127],
                info: {
                    name: "正德楼",
                    type: "办公楼",
                    faultCount: 6,
                    lastFault: "2024-12-28",
                    faultType: "一般故障",
                    status: "已完成"
                }
            },
            { 
                name: "笃行楼A栋", 
                position: [106.640213, 26.377083],
                info: {
                    name: "笃行楼A栋",
                    type: "教学楼",
                    faultCount: 9,
                    lastFault: "2025-01-03",
                    faultType: "电力故障",
                    status: "处理中"
                }
            }
        ];

        // 创建信息窗体
        const infoWindow = new AMap.InfoWindow({
            isCustom: true,
            content: '',
            offset: new AMap.Pixel(0, -30)
        });

        // 添加建筑标记
        buildings.forEach(building => {
            const marker = new AMap.Marker({
                position: building.position,
                title: building.name,
                map: map,
                icon: new AMap.Icon({
                    size: new AMap.Size(32, 32),
                    image: 'data:image/svg+xml;base64,' + btoa(`
                        <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
                            <circle cx="16" cy="16" r="12" fill="#ff4757" stroke="#fff" stroke-width="2"/>
                            <circle cx="16" cy="16" r="6" fill="#fff"/>
                            <text x="16" y="20" text-anchor="middle" fill="#ff4757" font-size="10" font-weight="bold">${building.info.faultCount}</text>
                        </svg>
                    `),
                    imageOffset: new AMap.Pixel(0, 0),
                    imageSize: new AMap.Size(32, 32)
                })
            });

            // 添加点击事件
            marker.on('click', function(e) {
                const info = building.info;
                const content = `
                    <div style="
                        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                        color: white;
                        padding: 15px;
                        border-radius: 8px;
                        box-shadow: 0 4px 15px rgba(0,0,0,0.3);
                        min-width: 250px;
                        font-family: 'Microsoft YaHei', sans-serif;
                    ">
                        <h3 style="margin: 0 0 10px 0; font-size: 16px; border-bottom: 1px solid rgba(255,255,255,0.3); padding-bottom: 8px;">
                            📍 ${info.name}
                        </h3>
                        <div style="font-size: 13px; line-height: 1.6;">
                            <p style="margin: 5px 0;"><strong>楼宇类型：</strong>${info.type}</p>
                            <p style="margin: 5px 0;"><strong>故障总数：</strong><span style="color: #ffd700;">${info.faultCount}次</span></p>
                            <p style="margin: 5px 0;"><strong>最近故障：</strong>${info.lastFault}</p>
                            <p style="margin: 5px 0;"><strong>故障类型：</strong>${info.faultType}</p>
                            <p style="margin: 5px 0;"><strong>处理状态：</strong>
                                <span style="
                                    color: ${info.status === '已完成' ? '#2ecc71' : '#f39c12'};
                                    font-weight: bold;
                                ">${info.status}</span>
                            </p>
                        </div>
                        <div style="text-align: center; margin-top: 10px;">
                            <button onclick="this.parentElement.parentElement.parentElement.style.display='none'" 
                                style="
                                    background: rgba(255,255,255,0.2);
                                    border: 1px solid rgba(255,255,255,0.3);
                                    color: white;
                                    padding: 5px 15px;
                                    border-radius: 4px;
                                    cursor: pointer;
                                    font-size: 12px;
                                ">关闭</button>
                        </div>
                    </div>
                `;
                
                infoWindow.setContent(content);
                infoWindow.open(map, marker.getPosition());
            });
        });

        // 生成围绕校园的热力点（故障点）
        const heatPoints = [];
        const campusCenter = [106.64366, 26.375555]; // 校园中心坐标

        // 生成80个围绕校园中心的热力点，增加密度
        for (let i = 0; i < 80; i++) {
            const lngOffset = (Math.random() - 0.5) * 0.008; // 减小范围使热力点更集中
            const latOffset = (Math.random() - 0.5) * 0.008; // 减小范围使热力点更集中
            const count = Math.floor(Math.random() * 15) + 5; // 随机热力值 5-20
            
            heatPoints.push({
                lng: campusCenter[0] + lngOffset,
                lat: campusCenter[1] + latOffset,
                count: count
            });
        }

        // 创建热力图
        const heatmap = new AMap.Heatmap(map, {
            radius: 25, // 热力点半径
            opacity: [0, 0.8], // 透明度范围
            gradient: {
                0.4: 'blue',
                0.6: 'cyan',
                0.7: 'lime',
                0.8: 'yellow',
                1.0: 'red'
            }
        });

        // 设置热力图数据
        heatmap.setDataSet({
            data: heatPoints,
            max: 20
        });

        // 确保热力图显示
        setTimeout(() => {
            heatmap.show();
        }, 1000);

        // 添加主题切换功能
        const themeSelector = document.getElementById('map-theme-selector');
        if (themeSelector) {
            themeSelector.addEventListener('change', function () {
                const selectedTheme = this.value;
                console.log('切换主题到:', selectedTheme); // 调试信息
                
                // 更新地图样式
                map.setMapStyle(`amap://styles/${selectedTheme}`);
                
                // 强制刷新地图
                setTimeout(() => {
                    map.getView().refresh();
                }, 100);
            });
        }

        // 地图加载完成后的回调
        map.on('complete', function() {
            console.log('地图加载完成');
            // 确保热力图在地图加载完成后显示
            heatmap.show();
        });
    });

    // 材料库存滚动效果
    document.addEventListener("DOMContentLoaded", function () {
        const scrollBox = document.querySelector('.scroll-box');
        if (scrollBox) {
            const scrollList = scrollBox.querySelector('.scroll-list');
            let currentPosition = 0;
            const itemHeight = 30; // 每个列表项的高度

            setInterval(() => {
                currentPosition -= 1; // 每次移动1像素
                const maxScroll = scrollList.children.length * itemHeight - scrollBox.offsetHeight;

                if (Math.abs(currentPosition) >= maxScroll) {
                    // 滚动到底部时重置位置
                    currentPosition = 0;
                }

                scrollList.style.transform = `translateY(${currentPosition}px)`;
            }, 50); // 每50毫秒移动一次，实现平滑滚动
        }
    });

